﻿using System;

namespace SoftwareConsulting.BI11.NumericalMethods.ComplexNumbers
{        
    /// <summary>
    /// Структура комплексного числа с двойной точностью
    /// </summary>
    public struct ComplexNumber
    {
        private double _x;
        private double _y;

        public double X
        {
            get
            {
                return _x;
            }
            set
            {
                _x = value;
            }
        }

        public double Y
        {
            get
            {
                return _y;
            }
            set
            {
                _y = value;
            }
        }       

        public ComplexNumber(double newX)
        {
            _x = newX;
            _y = 0;
        }

        public ComplexNumber(double newX, double newY)
        {
            _x = newX;
            _y = newY;
        }
        public static implicit operator ComplexNumber(double x)
        {
            return new ComplexNumber(x);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public static bool operator ==(ComplexNumber lhs, ComplexNumber rhs)
        {
            return (lhs.X == rhs.X) & (lhs.Y == rhs.Y);
        }
        public static bool operator !=(ComplexNumber lhs, ComplexNumber rhs)
        {
            return (lhs.X != rhs.X) | (lhs.Y != rhs.Y);
        }
        public static ComplexNumber operator +(ComplexNumber lhs)
        {
            return lhs;
        }
        public static ComplexNumber operator -(ComplexNumber lhs)
        {
            return new ComplexNumber(-lhs.X, -lhs.Y);
        }
        public static ComplexNumber operator +(ComplexNumber lhs, ComplexNumber rhs)
        {
            return new ComplexNumber(lhs.X + rhs.X, lhs.Y + rhs.Y);
        }
        public static ComplexNumber operator -(ComplexNumber lhs, ComplexNumber rhs)
        {
            return new ComplexNumber(lhs.X - rhs.X, lhs.Y - rhs.Y);
        }
        public static ComplexNumber operator *(ComplexNumber lhs, ComplexNumber rhs)
        {
            return new ComplexNumber(lhs.X * rhs.X - lhs.Y * rhs.Y, lhs.X * rhs.Y + lhs.Y * rhs.X);
        }
        public static ComplexNumber operator /(ComplexNumber lhs, ComplexNumber rhs)
        {
            ComplexNumber result = 0;
            double e;
            double f;            
            if (Math.Abs(rhs.Y) < Math.Abs(rhs.X))
            {
                e = rhs.Y / rhs.X;
                f = rhs.X + rhs.Y * e;
                result.X = (lhs.X + lhs.Y * e) / f;
                result.Y = (lhs.Y - lhs.X * e) / f;
            }
            else
            {
                e = rhs.X / rhs.Y;
                f = rhs.Y + rhs.X * e;
                result.X = (lhs.Y + lhs.X * e) / f;
                result.Y = (-lhs.X + lhs.Y * e) / f;
            }
            return result;
        }
    }    
}
